package com.yonyou.gmmc.common.service;

import java.util.Date;

import org.marketing.common.event.BizEvent;
import org.marketing.common.event.HandleResult;
import org.marketing.common.util.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
//import com.dianping.cat.Cat;
//import com.dianping.cat.message.Transaction;
//import com.yonyou.gmmc.common.cat.MyContext;
import com.yonyou.gmmc.common.eventhandler.AbstractEventHandler;

@Service
public class BaseService {
	
	private  Logger logger = LoggerFactory.getLogger(this.getClass());
	
	//app.appointmentInfo 
	//dcms.appointmentStatus 
	//dcms.maintainOrder 
	//app.potentialInfo 
	//app.recommandOrder 
	//dcms.recommandOrderResult 
	public enum MQChannelType {
		eventChannel("eventExchange"),//通用的事件通道
		appAppointmentInfo("app.appointmentInfo"), //app对外发送预约
		dcmsAppointmentStatus("dcms.appointmentStatus"), //dcms发送预约确认
		dcmsMaintainOrder("dcms.maintainOrder"), //dcms发送维修工单
		appPotentialInfo("app.potentialInfo"), //app发送潜客留资
		appRecommandOrder("app.recommandOrder"), //app发送推荐购车
		dcmsRecommandOrderResult("dcms.recommandOrderResult"),	//dcms发送推荐购车反馈
        appShoppingPoints("app.shoppingPoints"), //app发送第三方扣分
        appTicket("app.bindTicket"), //自动发放卡券
        appSendComplaint("app.complaint"), //app发送投诉建议
        appSendCustomerData("app.customerData") //app发送客户留资数据到DCMS
        ;

		private final String type;

		MQChannelType(String type) {
			this.type = type;
		}

		public String toString() {
			return type.toString();
		}
		
	}

	@Autowired
	com.yonyou.gmmc.common.feign.MqInterface mqInterface;

	/**
	 * 发送事件到mq
	 * 
	 * @param event
	 * @return
	 * @throws Exception
	 */
	public String sendEvent2MQ(BizEvent event) throws Exception {

		if (event.getEventKey()==null ||event.getEventKey().equals("")) {
			event.setEventKey(String.valueOf(new Date().getTime()));
		}
		String s = "";
//		Transaction t = Cat.newTransaction("method", "sendEvent2MQ");
//		Cat.logEvent("sendEvent2MQ", "invoke");
//		MyContext ctx = new MyContext();
//		 Cat.logRemoteCallClient(ctx);
//		 Map<String,String> proMap =ctx.getProperties();
		try {//MQChannelType.eventChannel.toString()
			s = mqInterface.sendEvent2MQ("event_"+event.getEventBizStep().getCode(), "999", event.getEventKey(), String.valueOf(new Date().getTime()),
					"999", JSON.toJSON(event).toString());
//			t.setStatus(Transaction.SUCCESS);
			//t.complete();
			return s;
		} catch (Exception e) {
//			t.setStatus(e);
//			Cat.logError(e);
			//e.printStackTrace();
			throw e;
		} finally {
//			t.complete();
		}
		
	}
	
	
	/**
	 * 发送消息到MQ的共用方法
	 * 
	 * @param channel 通道名称
	 * @param jsonBody 业务数据
	 * @return
	 * @throws Exception
	 */
	public String sendMessage2MQ(MQChannelType channel,String jsonBody) throws Exception {
		String s = "";
//		Transaction t = Cat.newTransaction("method", "sendMessage2MQ");
//		Cat.logEvent("sendEvent2MQ", "invoke");
//		MyContext ctx = new MyContext();
//		 Cat.logRemoteCallClient(ctx);
//		 Map<String,String> proMap =ctx.getProperties();
		try {
			s = mqInterface.sendEvent2MQ(channel.toString(), "999", String.valueOf(new Date().getTime()), String.valueOf(new Date().getTime()),
					"999", jsonBody);
//			t.setStatus(Transaction.SUCCESS);
			//t.complete();
			return s;
		} catch (Exception e) {
//			t.setStatus(e);
//			Cat.logError(e);
			//代码扫描 7 10.18
			//e.printStackTrace();
			throw e;
		} finally {
//			t.complete();
		}
		//return s;
	}
	
	
	

	public String sendEvent2MQ2(BizEvent event) throws Exception {

		if (event.getEventKey().equals("")) {
			event.setEventKey(String.valueOf(new Date().getTime()));
		}
		String s = "";
//		Map<String,String> proMap =new HashMap<>();
		try {
			s = mqInterface.sendEvent2MQ("exchange2", "999", event.getEventKey(), String.valueOf(new Date().getTime()),
					"999", JSON.toJSON(event).toString());
			return s;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
		return s;
	}
	
	
	
	/**
	 * 根据事件获取处理类
	 * 
	 * @param event
	 * @return
	 */
	public AbstractEventHandler getEventHandler(BizEvent event){
			AbstractEventHandler handle = null;
			
			try {
				logger.info("使用spring实例化处理的类==="+event.getEventBizStep().toString());
				handle = (AbstractEventHandler)SpringUtil.getBean(event.getEventBizStep().toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				logger.warn("事件====="+event.getEventBizStep().toString()+"======获取事件对应处理类失败",e);
				return null;
			}
			return handle;
	}
	
	
	/**
	 *	处理相关事件
	 *
	 * @param event
	 * @return
	 */
	public HandleResult handleEvent(BizEvent event){
		AbstractEventHandler handle = getEventHandler(event);
		
		if(handle==null){
			HandleResult result=new HandleResult();
			result.setHandle(true);
			result.setMessage("不处理该消息");
			return result;
		}
		
		return handle.handleing(event);
	}

}
